const http = require('http')
const url = require('url')

const server = http.createServer((req, res) => {
  /**
   * >>>>>>>>>> 请求方式 url <<<<<<<<<<
   * http://localhost:1234 ==> /
   * http://localhost:1234/index.html?id=123 ==> /index.html?id=123
   *  console.info('req.url:', req.url)
   */
  let { pathname, query } = url.parse(req.url, true)
  console.info('pathname:', pathname)
  console.info('query:', query)

  // >>>>>>>>>> 请求方式: method <<<<<<<<<<
  console.info('req.method:', req.method) // GET | POST

  // >>>>>>>>>> 版本号: httpVersion <<<<<<<<<<
  console.info('req.httpVersion:', req.httpVersion) // 1.1

  // >>>>>>>>>> 请求头: headers <<<<<<<<<<
  console.info('req.headers:', req.headers) // 1.1

  // >>>>>>>>>> 请求体: body <<<<<<<<<<
  let arr = []
  req.on('data', (data) => {
    arr.push(data)
  })

  req.on('end', () => {
    console.info(Buffer.concat(arr).toString())
  })
})

server.listen(1234, () => {
  console.info(`服务端启动了 http://localhost:1234`)
})
